package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;


    @Autowired
    private FactoryDao factoryDao;

    @Override
    public void save(ContractProduct contractProduct) {
        //1.计算货物总金额
        double money = 0;

        money = contractProduct.getCnumber() * contractProduct.getPrice();

        //2设置货物的总金额
          contractProduct.setAmount(money);
        //3.保存货物
          contractProductDao.insertSelective(contractProduct);
        //4.根据id查询购销合同
          Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //5设置购销合同总金额
          contract.setTotalAmount(contract.getTotalAmount() + money);
        //6.设置购销合同货物数量
          contract.setProNum(contract.getProNum() + 1);
        //7.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        /**
         * 更新货物
         *  1.计算修改之后的货物总金额
         *  2.查询修改之前的货物总金额
         *  3.设置货物的总金额
         *  4.更新货物
         *  5.查询购销合同
         *  6.设置购销合同总金额
         *  7.更新购销合同
         *
         */
        //1.计算修改之后的货物总金额
        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
        Double amount_new =  price*cnumber;
        //2  查询修改之前的货物总金额
        ContractProduct product = contractProductDao.selectByPrimaryKey(contractProduct.getId());//根据要修改的合同id 查询原合同的金额
        Double amount_old = product.getAmount();
        //3.设置货物的总金额
        contractProduct.setAmount(amount_new);
        //4.更新货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //5.查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
         //6.设置购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount()+amount_new-amount_old);
         //7.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void delete(String id) {//货物id
        //1.查询货物对象
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        // 2.查询此货物下所有附件
        //2.1 构造查询条件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(id);
        //返回附件集合
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        // 3.查询购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        // 4.计算总金额 (货物金额 + 所有附件金额)
        double money = contractProduct.getAmount();//货物小计
        //求出货物小计加上 所有附件小计
        double money_extCproduct=0;//初始化附件金额
        for (ExtCproduct extCproduct : extCproducts) {
            money_extCproduct+=extCproduct.getAmount();
            // 5.删除附件
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        // 6.删除货物/
        contractProductDao.deleteByPrimaryKey(id);
        // 7.设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-money-money_extCproduct);
        // 8.设置附件和货物数量
        //8.1设置货物数量-1
        contract.setProNum(contract.getProNum()-1);
         //8.1设置附件数量  -附件集合长度
        contract.setExtNum(contract.getExtNum()-extCproducts.size());
        // 9.更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> pageInfo = contractProductDao.selectByExample(example);
        return new PageInfo<ContractProduct>(pageInfo,5);
    }

    @Override
    public void saveList(List<ContractProduct> productList) {

        //获取工厂id
        FactoryExample factoryExample = null;
        for (ContractProduct contractProduct : productList) {
            factoryExample =new FactoryExample();
            factoryExample.createCriteria().andFactoryNameEqualTo(contractProduct.getFactoryName());

            List<Factory> factories = factoryDao.selectByExample(factoryExample);

            if(factories!=null&&factories.size()>0){
                   contractProduct.setFactoryId(factories.get(0).getId());
                }

                //计算上传的货物的小计金额
            this.save(contractProduct);
        }
    }
}
